﻿using Bank.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bank.Operations;

namespace Bank
{
    public class Client
    {
        public Client()
        {
            lastname = "";
            fistname = "";
            pesel = "";
            blocked = false;
            manager = new AccountManager();
            accounts = new List<String>();

        }

        public Client(String fistName, String lastName , String Pesel)
        {
            lastname = lastName;
            fistname = fistName;
            pesel = Pesel;
            blocked = false;
            manager = new AccountManager();
            accounts = new List<String>();
        }

        public IAccount requestNewAccount(AccountType type, Int32 clientID, decimal ballance)
        {
            if (blocked == false)
            {
                IAccount newAccount = manager.createNewAccount(type, clientID, ballance);
                if (newAccount != null) 
                     accounts.Add(newAccount.Number);                     
                else  
                    throw new Exception("Cant create account");

                return newAccount;
            }
            else
            {
                throw new BlockedAccountException("Client: " + FirstName + " " + LastName + " have blocked account");
            }
        }

        public IAccount requestNewAccount(AccountType type, Int32 clientID, Money ballance)
        {
            if (blocked == false)
            {
                IAccount newAccount = manager.createNewAccount(type, clientID, ballance);
                if (newAccount != null)
                    accounts.Add(newAccount.Number);
                else
                    throw new Exception("Cant create account");

                return newAccount;
            }
            else
            {
                throw new BlockedAccountException("Client: " + FirstName + " " + LastName + " have blocked account");
            }
        }


        public void makePayment(decimal amount, String accountID)
        {
            if (blocked == false)
                manager.makePayment(amount, accountID, this.ClientID);
            else
            {
                throw new BlockedAccountException("Client: " + FirstName + " " + LastName + " have blocked account");
            }
        }

        public void makePayment(Money amount, String accountID)
        {
            if (blocked == false)
                manager.makePayment(amount, accountID, this.ClientID);
            else
            {
                throw new BlockedAccountException("Client: " + FirstName + " " + LastName + " have blocked account");
            }
        }

        public void showClientInformation()
        {
            Console.WriteLine("Client Information");
            Console.WriteLine("FirstName: " + FirstName);
            Console.WriteLine("LastName: " + LastName);
            Console.WriteLine("Pesel: " + Pesel);
            Console.WriteLine("All Accounts:");
            if (Accounts != null)
                foreach( String account in Accounts) {
                    Console.WriteLine("Type of account " + account);               
                }
            Console.WriteLine("");
        }

        public void changeInterestCalculator(InterestCalculatorType type, string AccountId)
        {
            switch (type)
            {
                case InterestCalculatorType.RangeInterestCalculator:
                    Manager.changeInterestCalculator(this.clientID, AccountId, new RangeInterestCalculator());
                    break;
                case InterestCalculatorType.SimpleInterestCalculator:
                    Manager.changeInterestCalculator(this.clientID, AccountId, new SimpleInterestCalculator());
                    break;              
            }
        }

        private String fistname;
        public String FirstName
        {
            get { return fistname; }
            set { fistname = value; }
        }

        private String lastname;
        public String LastName
        {
            get { return lastname; }
            set { lastname = value; }
        }

        private String pesel;
        public String Pesel
        {
            get { return pesel; }
            set { pesel = value; }
        }
        

        private Int32 clientID;
        public Int32 ClientID
        {
            get { return clientID; }
            set { clientID = value; }
        }        

        private AccountManager manager;
        public AccountManager Manager
        {
            get { return manager; }
            set { manager = value; }
        }        

        private List<String> accounts;
        public List<String> Accounts
        {
            get { return accounts; }
            set { accounts = value; }
        }

        private Boolean blocked;
        public Boolean Blocked
        {
            get { return blocked; }
            set { blocked = value; }
        }
        

    }
      
}